<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Subject as modelSubject;
use think\Db;
class Subject extends Common
{
    function index()
    {
        $subjectList = modelSubject::subjectList();

//        dump($subjectList);exit;
        $this->assign('list', $subjectList);
        return $this->fetch('index');
    }

    function subjectAdd()
    {
        if (request()->isAjax()) {
            $data = modelSubject::disposeData(input('post.'));
            $subjectid = input('post.subjectid');
            if($subjectid > 0){
                $res = Db::name('subject')->where("subjectid",$subjectid)->update($data);
            }else{
                $res = Db::name('subject')->insertGetId($data);
            }
            if ($res) {
                exitjson(1, '保存成功');
            } else {
                exitjson(0, '保存失败');
            }
        } else {
            if(input('id') > 0){
                $one = Db::name('subject')->find(input('id'));
                $this->assign('one',$one);
            }
//            dump($one);exit;
            return $this->fetch('subjectAdd');
        }
    }

    function subjectDel(){
        $res = Db::name('subject')->delete(input('id'));
        if($res){
            exitjson(1,'删除成功');
        }else{
            exitjson(0,'删除失败');
        }
    }

}